/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agenda.model.persistence;

import com.agenda.model.entity.AmTbUsuaSist;
import com.agenda.model.repository.UsuarioRepository;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Ronildo
 */
public class UsuarioDAO extends GenericDAO<AmTbUsuaSist, UsuarioRepository> implements UsuarioRepository {

    @Override
    public AmTbUsuaSist findById(Long id) {
        return (AmTbUsuaSist)em.createNamedQuery("AmTbUsuaSist.findByCdUsua").
                setParameter("cdUsua", id).getSingleResult();
    }

    @Override
    public List<AmTbUsuaSist> list(AmTbUsuaSist amTbUsuaSist) {
        try{
            String hql = "select usuario from AmTbUsuaSist as usuario ";
            hql+=" where usuario.cdUsua = usuario.cdUsua";
            
            if(amTbUsuaSist.getNmEmaiUsua() != null && amTbUsuaSist.getNmEmaiUsua().equals(""))
                hql+=" and usuario.nmEmaiUsua like :nmEmaiUsua";
            if(amTbUsuaSist.getDtUltiAces() != null)
                hql+=" and dtUltiAces = :dtUltiAces";
            if(amTbUsuaSist.getDtCada() != null)
                hql+=" and dtCada = :dtCada";
            hql+=" order by usuario.cdUsua";
            
            Query query = em.createQuery(hql);
            if(amTbUsuaSist.getNmEmaiUsua() != null && amTbUsuaSist.getNmEmaiUsua().equals(""))
                query.setParameter("nmMedico", "%"+amTbUsuaSist.getNmEmaiUsua()+"%");
            if(amTbUsuaSist.getDtUltiAces() != null)
                query.setParameter("dtUltiAces", amTbUsuaSist.getDtUltiAces());
            if(amTbUsuaSist.getDtCada() != null)
                query.setParameter("dtCada", amTbUsuaSist.getDtCada());
                
            return query.getResultList();
        }catch(RuntimeException e){
            throw new DAOException(e);
        }
    }

    @Override
    public List<AmTbUsuaSist> listAll() {
        return em.createNamedQuery("AmTbUsuaSist.findAll").getResultList();
    }
}
